package com.astrofizzbizz.stoneedgegui.monitor;

import com.astrofizzbizz.stoneedgegui.app.WaitFrame;
import com.astrofizzbizz.stoneedgegui.utilities.StoneEdgeIIIException;


public class DomeControl implements Runnable
{
	private TelescopeMonitorPanel telescopeMonitorPanel;
	private boolean openDome = false;
	
	public DomeControl(TelescopeMonitorPanel telescopeMonitorPanel, boolean openDome)
	{
	    this.telescopeMonitorPanel = telescopeMonitorPanel;
	    this.openDome = openDome;
	}
	public void openDome()
	{
		String openCommand = "openup nocloud user=remote";
		WaitFrame waitFrame = null;
	    try 
	    {
			waitFrame = new WaitFrame("Wait", "Please wait. Opening Dome", telescopeMonitorPanel.getStoneEdgeIIIGui());
	    	telescopeMonitorPanel.getDomeMonitorPanel().getDomeOpenCloseButton()[0].setVisible(false);
	    	telescopeMonitorPanel.getStatusBar().setText("Opening Dome");
			telescopeMonitorPanel.getStoneEdgeIIIGui().getStoneEdgeIIITunnel().executeCommand(openCommand, 1024);
			telescopeMonitorPanel.getDomeMonitorPanel().getDomeMonitor().setKeepTrackingOn(true);
	    	telescopeMonitorPanel.getDomeMonitorPanel().getDomeMonitor().updateMonitor();
			
			telescopeMonitorPanel.getStatusBar().setText("Dome Open");
	    	telescopeMonitorPanel.getDomeMonitorPanel().getDomeOpenCloseButton()[1].setVisible(true);
//	    	telescopeMonitorPanel.getDomeCameraPanel().messageDialog("Dome Open");
			waitFrame.dispose();
		} 
	    catch (StoneEdgeIIIException e) 
		{
	    	waitFrame.dispose();
			e.messageDialog(telescopeMonitorPanel.getStoneEdgeIIIGui());
			
		}
		
	}
	public void closeDome()
	{
		String openCommand = "closedown user=remote";
		WaitFrame waitFrame = null;
	    try 
	    {
			waitFrame = new WaitFrame("Wait", "Please wait. Closing Dome", telescopeMonitorPanel.getStoneEdgeIIIGui());
	    	telescopeMonitorPanel.getDomeMonitorPanel().getDomeOpenCloseButton()[0].setVisible(false);
	    	telescopeMonitorPanel.getDomeMonitorPanel().getDomeOpenCloseButton()[1].setVisible(false);

	    	telescopeMonitorPanel.getStatusBar().setText("Closing Dome");
			telescopeMonitorPanel.getStoneEdgeIIIGui().getStoneEdgeIIITunnel().executeCommand(openCommand, 1024);
			telescopeMonitorPanel.getDomeMonitorPanel().getDomeMonitor().setKeepTrackingOn(false);
	    	telescopeMonitorPanel.getDomeMonitorPanel().getDomeMonitor().updateMonitor();
			telescopeMonitorPanel.getStatusBar().setText("Dome Closed");
	    	telescopeMonitorPanel.getDomeMonitorPanel().getDomeControlPanel().setVisible(false);
	    	telescopeMonitorPanel.getDomeMonitorPanel().getDomeOpenCloseButton()[0].setVisible(true);
//	    	telescopeMonitorPanel.getDomeCameraPanel().messageDialog("Dome Closed");
			waitFrame.dispose();
		} 
	    catch (StoneEdgeIIIException e) 
		{
			waitFrame.dispose();
			e.messageDialog(telescopeMonitorPanel.getStoneEdgeIIIGui());
		}
		
	}
	@Override
	public void run() 
	{
		if (openDome) 
		{
			openDome();
		}
		if (!openDome)
		{
			closeDome();
		}
	}

}
